package fr.umlv.ig.infoView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.umlv.ig.event.TrainEvent;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Train;

@SuppressWarnings("serial")
public class TrainInfoView extends JPanel {
	
	private final MetroModelImpl metroModel;
	
	private static final String trainString = new String("Train : ");
	private static final String stationString = new String("Station : ");
	private static final String nextString = new String("Next Station : ");
	private static final String labelTerminusStationString = new String("Terminus : ");
	private static final String labelStateRemoveString = new String("State : is removed");
	private static final String labelStateAddString = new String("State : is added");
	private static final String labelIsMovingString = new String("State : is moving ");
	private static final String labelIsStoppedString = new String("State : is stopped ");
	
	private final  JLabel labelId = new JLabel(trainString);
	private final JLabel labelCurrentStation = new JLabel(stationString);
	private final JLabel labelNextStation = new JLabel(nextString);
	private final JLabel labelTerminusStation = new JLabel(labelTerminusStationString);
	private final JLabel labelState = new JLabel();
	private final JButton removeButton = new JButton("remove train");
	
	private Train train;

	public TrainInfoView(final MetroModelImpl metroModel) {
		super();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(Box.createVerticalStrut(10));
		add(labelId);
		add(Box.createVerticalStrut(7));
		add(labelCurrentStation);
		add(Box.createVerticalStrut(7));
		add(labelNextStation);
		add(Box.createVerticalStrut(7));
		add(labelTerminusStation);
		add(Box.createVerticalStrut(7));
		add(removeButton);
		add(Box.createVerticalStrut(7));
		add(labelState);
		this.metroModel = metroModel;

		removeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("train removed : " + train.getId());
				
				metroModel.removeTrain(train);
				removeButton.setEnabled(false);
			}
	
		});
		
	}
	
	public void setInfo(TrainEvent eventTrain) {
		
		removeButton.setEnabled(true);
		train = (Train) eventTrain.getSource(); 
		labelId.setText(trainString + train.getId());
		labelCurrentStation.setText(stationString + metroModel.getCurrentStation(train).getName());
		labelNextStation.setText(nextString + metroModel.getNextStation(train).getName());
		labelTerminusStation.setText(labelTerminusStationString + metroModel.getTerminus(train).getName());
		if (eventTrain.getEventType() == TrainEvent.TRAIN_REMOVE){
			labelState.setText(labelStateRemoveString);
		}
		
		else if(eventTrain.getEventType() == TrainEvent.TRAIN_ADDED) {
			labelState.setText(labelStateAddString);
		}
		
		else if(metroModel.isMoving(train)) {
			labelState.setText(labelIsMovingString);
		}
		else {
			labelState.setText(labelIsStoppedString);
		}
		repaint();
	}


}
